package com.itqf.service.impl;

import com.itqf.bean.Bill;
import com.itqf.bean.Param;
import com.itqf.bean.Provider;
import com.itqf.mapper.BillMapper;
import com.itqf.service.BillService;
import com.itqf.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * projectName: springmvc2103
 *
 * @author: 赵伟风
 * time: 2021/7/5 20:30
 * description:
 */
@Service
public class BillServiceImpl implements BillService {

    @Autowired
    private BillMapper billMapper;

    @Override
    public ResultVO findProviders() {
        //1.处理参数
        //2.调用数据
        List<Provider> providers = billMapper.queryProviders();
        //3.处理数据库数据
        ResultVO resultVO = null;
        if (providers != null && providers.size() >0) {
            resultVO = ResultVO.OK(providers);
        }else{
            resultVO = ResultVO.FAIL("未查询到供应商数据！");
        }
        return resultVO;
    }

    @Override
    public ResultVO findBills() {

        List<Bill> bills = billMapper.queryBills();

        ResultVO resultVO = null;

        if (bills != null && bills.size() > 0) {
            resultVO = ResultVO.OK(bills);
        }else{
            resultVO = ResultVO.FAIL("未查询到账单数据！");
        }
        return resultVO;
    }

    @Override
    public ResultVO saveBill(Bill bill) {

        int rows = billMapper.insertBill(bill);

        ResultVO resultVO = null;
        if (rows>0) {
            resultVO = ResultVO.OK("数据插入成功！");
        }else{
            resultVO= ResultVO.FAIL("数据插入失败！");
        }
        return resultVO;
    }

    @Override
    public ResultVO updateBill(Bill bill) {

        //dml 返回都是int
        int rows = billMapper.updateBill(bill);

        ResultVO resultVO = null;

        if (rows>0){
            resultVO = ResultVO.OK("更新数据成功！");
        }else{
            resultVO = ResultVO.FAIL("更新数据失败！");
        }

        return resultVO;
    }

    @Override
    public ResultVO infoBill(int id) {

        Bill bill = billMapper.queryById(id);

        ResultVO resultVO = null;
        if (bill != null) {
            resultVO = ResultVO.OK(bill);
        }else{
            resultVO = ResultVO.FAIL("未查询到对应的数据！");
        }

        return resultVO;
    }

    @Override
    public ResultVO removeBill(int id) {

        int rows = billMapper.deleteById(id);

        ResultVO resultVO = null;

        if (rows>0) {
            resultVO = ResultVO.OK("删除数据成功！");
        }else{
            resultVO = ResultVO.FAIL("删除数据失败！！");
        }

        return resultVO;
    }

    @Override
    public ResultVO searchBill(Param param) {

        List<Bill> bills = billMapper.queryByParam(param);

        ResultVO resultVO = null;

        if (bills != null && bills.size()>0) {
            resultVO = ResultVO.OK(bills);
        }else {
            resultVO = ResultVO.FAIL("查询数据失败！");
        }

        return resultVO;
    }
}
